﻿using System.Linq;

namespace ComeSeeThis_MVC2.Models
{
    public class VehicleSpurRepository
    {
        private readonly ComeSeeThisDBEntities _entities = new ComeSeeThisDBEntities();

        #region Query Methods

        public IQueryable<VehicleSpur> GetAllVehicleSpurs()
        {
          return  _entities.VehicleSpurs.OrderBy(x=>x.SpurGear);
        }

        public IQueryable<VehicleSpur> GetAllVehicleSpursByVehicleId(int vehicleId)
        {
            var boo = _entities.VehicleSpurs.Where(x => x.VehicleId == vehicleId);
            return boo.OrderBy(x => x.SpurGear);
        }

        public VehicleSpur GetVehicleSpur(int vehicleSpurId)
        {
            return _entities.VehicleSpurs.FirstOrDefault(c => c.VehicleSpurId == vehicleSpurId);
        }

        public bool SpurExists(int vehicleId, int spur)
        {
            var results =_entities.VehicleSpurs.Where(c => c.VehicleId == vehicleId).Where(x => x.SpurGear == spur);
            var boo = results.Count(x => x.SpurGear != 0);
            return boo != 0;
        }

        #endregion

        #region Add/Delete

        public void Add(VehicleSpur vehicleSpur)
        {
            _entities.VehicleSpurs.AddObject(vehicleSpur);
        }

        public void Delete(VehicleSpur vehicleSpur)
        {
            _entities.VehicleSpurs.DeleteObject(vehicleSpur);
        }

        #endregion

        #region Persistance

        public void Save()
        {
            _entities.SaveChanges();
        }

        #endregion
    }
}